package com.mhs.DAO;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.mhs.DTO.MySQLiteHelper;
import com.mhs.DTO.PhanLoaiThuocDTO;

public class PhanLoaiThuocDAO {
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	public PhanLoaiThuocDAO(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	
	public void close() {
		dbHelper.close();
	}

	public PhanLoaiThuocDTO getPhanLoaiThuocFromMaLoaiThuoc(int maloaithuoc) {
		PhanLoaiThuocDTO phanloaithuoc = new PhanLoaiThuocDTO();
		try{			
			String sQuery = "select maloaithuocclient, maloaithuoc, tenloaithuoc, ghichu from phanloaithuoc where maloaithuoc = " + maloaithuoc + ";";
			database = dbHelper.getReadableDatabase();
			Cursor cursor = database.rawQuery(sQuery, new String [] {});
			if(cursor.getCount() == 0){
				phanloaithuoc = null;
			}else if(cursor.getCount() == 1){
				cursor.moveToFirst();
				while (!cursor.isAfterLast()) {
					phanloaithuoc.setMALOAITHUOC_CLIENT(cursor.getInt(0));
					phanloaithuoc.setMALOAITHUOC(cursor.getInt(1));
					phanloaithuoc.setTENLOAITHUOC(cursor.getString(2));
					phanloaithuoc.setGHICHU(cursor.getString(3));
					break;
				}
				cursor.close();
			}
		}catch(SQLException ex){
			ex.printStackTrace();
			phanloaithuoc = new PhanLoaiThuocDTO();
		}finally{
			database.close();
		}
		return phanloaithuoc;
	}
}
